package com.qs.eggs.view.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.utils.CityDialogHelper;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class AddAddress1Activity extends BaseActivity implements View.OnClickListener{
    private RelativeLayout fanhui;
    private EditText address;
    private ToggleButton togbtn;
    private  Button baocun;
    private LinearLayout mProvince,mCity,mCounty,mCommunity,mHeader;
    private TextView tvProvince,tvCity,tvCounty,tvCommunity,tvHeader;
    private ArrayList<HashMap<String,String>> mList;
    //三个地址选择哪个的标识
    private String flag="1";
    private String firstId="",secondId="",thredId="";
    private TextView tvForIdP,tvForIdCity,tvForIdCounty,tvForIdCommunity,tvForIdHeader;
    private String ProvinceId="",CityId="",CountyId="",CommunityId="",HeaderId="";
    private String xingming,number;
    private TextView name,phone;

    @Override
    protected int getContentView() {
        return R.layout.activity_add_address1;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        initView();
        getInfo();
    }

    private void initView(){
        fanhui=(RelativeLayout)findViewById(R.id.fanhui);
        name=(TextView)findViewById(R.id.et_name);
        phone=(TextView)findViewById(R.id.et_phone);
        address=(EditText)findViewById(R.id.et_address);

        tvProvince=(TextView)findViewById(R.id.tv_province);
        tvCity=(TextView)findViewById(R.id.tv_city);
        tvCounty=(TextView)findViewById(R.id.tv_county);
        tvCommunity=(TextView)findViewById(R.id.tv_community);
        tvHeader=(TextView)findViewById(R.id.tv_header);

        tvForIdP=(TextView)findViewById(R.id.tv_forid_p);
        tvForIdCity=(TextView)findViewById(R.id.tv_forid_city);
        tvForIdCounty=(TextView)findViewById(R.id.tv_forid_county);
        tvForIdCommunity=(TextView)findViewById(R.id.tv_forid_ommunity);
        tvForIdHeader=(TextView)findViewById(R.id.tv_forid_header);

        mProvince=(LinearLayout)findViewById(R.id.lly_province);
        mCity=(LinearLayout)findViewById(R.id.lly_city);
        mCounty=(LinearLayout)findViewById(R.id.lly_county);
        mCommunity=(LinearLayout)findViewById(R.id.lly_community);
        mHeader=(LinearLayout)findViewById(R.id.lly_header);

        togbtn=(ToggleButton)findViewById(R.id.mTogBtn);
        baocun=(Button)findViewById(R.id.save);
        fanhui.setOnClickListener(this);
        mProvince.setOnClickListener(this);
        mCity.setOnClickListener(this);
        mCounty.setOnClickListener(this);
        mCommunity.setOnClickListener(this);
        mHeader.setOnClickListener(this);
        baocun.setOnClickListener(this);
    }
    //获得个人信息
    private void getInfo(){
        OkGo.post(Constants.URL_BASE + "personal?action=getPersonalInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("个人信息==" + s);
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                xingming = jsonArray.getJSONObject(i).getString("name");
                                number = jsonArray.getJSONObject(i).getString("phone");
                                name.setText(xingming);
                                phone.setText(number);

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lly_province:
            HttpApi.getProvince(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    try {
                        ArrayList<String> mProvincesList=new ArrayList<String>();
                        ArrayList<String> mProvincesListId=new ArrayList<String>();
                        JSONArray jsonArray=new JSONArray(s);
                        for (int i=0;i<jsonArray.length();i++){
                            mProvincesList.add(jsonArray.getJSONObject(i).getString("areaname"));
                            mProvincesListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                        }

                        CityDialogHelper.getCity(AddAddress1Activity.this,mProvincesList,mProvincesListId,tvProvince,tvForIdP);
                        tvCity.setText("");
                        tvCounty.setText("");
                        tvCommunity.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            break;
            //加载市列表
            case R.id.lly_city:
                if(tvProvince.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddress1Activity.this,"请先选择省",Toast.LENGTH_SHORT).show();
                    return;
                }
                ProvinceId=tvForIdP.getText().toString();
                HttpApi.getCity(ProvinceId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCityList=new ArrayList<String>();
                            ArrayList<String> mCityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddress1Activity.this,mCityList,mCityListId,tvCity,tvForIdCity);
                            tvCounty.setText("");
                            tvCommunity.setText("");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }


                });
                break;

            //加载县列表
            case R.id.lly_county:
                if(tvCity.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddress1Activity.this,"请先选择市",Toast.LENGTH_SHORT).show();
                    return;
                }
                CityId=tvForIdCity.getText().toString();
                HttpApi.getCounty(CityId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            ArrayList<String> mCountyList=new ArrayList<String>();
                            ArrayList<String> mCountyListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                mCountyList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCountyListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddress1Activity.this,mCountyList,mCountyListId,tvCounty,tvForIdCounty);
                            tvCommunity.setText("");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            //加载小区列表
            case R.id.lly_community:
                if(tvCounty.getText().toString().trim().length()==0){
                    Toast.makeText(AddAddress1Activity.this,"请先选择县",Toast.LENGTH_SHORT).show();
                    return;
                }
                CountyId=tvForIdCounty.getText().toString();
                HttpApi.getCommunity(CountyId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("小区id=="+s);

                        try {
                            ArrayList<String> mCommunityList=new ArrayList<String>();
                            ArrayList<String> mCommunityListId=new ArrayList<String>();
                            JSONArray jsonArray=new JSONArray(s);
                            for (int i=0;i<jsonArray.length();i++){
                                CommunityId=jsonArray.getJSONObject(0).getString("areaid");
                                mCommunityList.add(jsonArray.getJSONObject(i).getString("areaname"));
                                mCommunityListId.add(jsonArray.getJSONObject(i).getString("areaid"));
                            }

                            CityDialogHelper.getCity(AddAddress1Activity.this,mCommunityList,mCommunityListId,tvCommunity,tvForIdCommunity);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                });
                break;
            case R.id.save:
                if (tvProvince.getText().equals("")||tvCity.getText().equals("")||tvCounty.getText().equals("")||tvCommunity.getText().equals("")||TextUtils.isEmpty(address.getText().toString())){
                    Toast.makeText(AddAddress1Activity.this,"信息不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    save();
                }

                break;
            case R.id.fanhui:
                AddAddress1Activity.this.finish();
                break;
        }
    }

    //保存地址
    private void save(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("provinceid",ProvinceId);
        map.put("cityid",CityId);
        map.put("areaid",CountyId);
        map.put("villageid",CommunityId);
        map.put("address",address.getText().toString());
        map.put("isdefault","1");
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"mallAddressManage?action=addCustAddress")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("添加地址==" + response);
                        if (s.equals("\"true\"")) {
                            AddAddress1Activity.this.finish();
                            //AddressManagement1Activity.instance.finish();
                            //Intent intent = new Intent(AddAddress1Activity.this, AddressManagement1Activity.class);
                            //startActivity(intent);

                        }
                    }

                });
      }
}